/**
 * @description  : 常用公共提示及文件下载
 * @descriptionDetail : 常用公共提示及文件下载
 * @copyright    : 浙江烟草
 * @author       : mc
 * @create       : 2022-10-04 16:42:50
 */
import deviceApi from "@/platforms/default.platform.service.js";
/** 
 * 公共的提示组件
 * 调用方式： this.$modal.alert, this.$modal.confirm
 */
export default {
  alert: function (content, title, successFun, confirmText, showCancel = false, errCallback) {
    if (typeof title === 'function') {
      ([title, successFun, confirmText,] = ["提示", title, successFun,]);
    }
    if (deviceApi.confirm) {
      if (showCancel) {
        deviceApi.confirm(content, title, [confirmText || '确定', '取消',], (index) => {
          if (index == 0) {
            successFun && successFun();
          } else {
            errCallback && errCallback();
          }
        },);
      } else {
        deviceApi.alert && deviceApi.alert(content, title, successFun);
      }
    } else {
      uni.showModal({
        title: title || '提示',
        content: content,
        showCancel: showCancel,
        confirmText: confirmText || "确定",
        success: function (res) {
          if (res.confirm) {
            successFun && successFun();
          } else {
            errCallback && errCallback();
          }
        },
      });
    }
  },
  confirm: function (content, title, successFun, failFun, confirmText, cancelText, confirmColor, cancelColor) {
    if (typeof title === 'function') {
      ([title, successFun, failFun, confirmText, cancelText,] = ["提示", title, successFun, failFun,
        confirmText,
      ]);
    }
    uni.showModal({
      title: title || '提示',
      content: content,
      showCancel: true,
      confirmText: confirmText || "确定",
      cancelText: cancelText || "取消",
      confirmColor: confirmColor || '#007aff',
      cancelColor: cancelColor || '#000000',
      success: function (res) {
        if (res.confirm) {
          successFun && successFun();
        } else if (res.cancel) {
          failFun && failFun();
        }
      },
    });
  },
  /**
   * title  loading 内容  
   * mask   是否点击外部区域可以取消loading   true 不可以。 false 可以
   */
  showLoading: function (title, mask = true) {
    uni.showLoading({
      title: title,
      success() { },
      mask: mask,
    });
  },
  /**
   * 根据provCode 判断是不是四个直辖市  北京、天津、重庆、上海
   */
  isMuniCity: function (provCode) {
    return provCode == '110000' || provCode == '120000' || provCode == '500000' || provCode == '310000';
  },

  /**
   */
  hideLoading: function () {
    uni.hideLoading();
  },

  /**
   * 提示toast
   */
  toast: function (str) {
    // #ifdef H5-DINGDING
    DingDing.toast(str);
    // #endif
    // #ifndef H5-DINGDING
    uni.showToast({
      title: str,
    });
    // #endif
  },

  downloadFile: function (url, fileName,size=0) {
    // #ifdef H5-ONLY
    let saveAs = function (data, name) {
      let urlObject = window.URL || window.webkitURL || window;
      let blobUrl = new Blob([data,]);
      let linkBtn = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
      linkBtn.href = urlObject.createObjectURL(blobUrl);
      linkBtn.download = name;
      linkBtn.click();
      uni.hideLoading();
    };
    let downFile = function (fileUrl, fileTmpName) {
      // 判断是否在微信
      let ua = navigator.userAgent.toLowerCase();
      let isWeixin = ua.indexOf('micromessenger') != -1;
      // 判断是否是钉钉
      let dingding = ua.indexOf('dingtalk') != -1;
      if (isWeixin || dingding) {
        // alert('在微信')
        uni.showToast({
          title: "请使用其他浏览器打开",
          icon: "none",
          duration: 2000,
        })
      } else {
        let url2 = fileUrl.replace(/\\/g, '/');
        let xhr = new XMLHttpRequest();
        xhr.open('GET', url2, true);
        xhr.responseType = 'blob';
        xhr.setRequestHeader("If-Modified-Since", "0");
        xhr.setRequestHeader('sessionid', uni.getStorageSync('sessionid'));
        // 为了避免大文件影响用户体验，建议加loading
        xhr.onload = function () {
          uni.showLoading({
            title: '加载中',
          });
          if (xhr.status === 200) {
            // 获取文件blob数据并保存
            saveAs(xhr.response, fileTmpName);
          } else {
            uni.hideLoading();
            uni.showToast({
              title: "下载文件失败",
              icon: "none",
              duration: 2000,
            })
          }
        };
        xhr.send();
      }
    };
    downFile(url, fileName);
    // #endif
    // #ifdef H5-DINGDING
    if (deviceApi.openLink) {
      deviceApi.openLink(url).then(res => {
        console.log(res);
      }).catch(e => {
        console.log(e);
      });
    }
    // #endif
		// #ifdef H5-WECHAT
		if (deviceApi.openLink) {
		  deviceApi.openLink(url,fileName,size);
		}
		// #endif
		
  },
  openNewTab(url) {
    if (deviceApi.openLink) {
      deviceApi.openLink(url).then(res => {
        console.log(res);
      }).catch(e => {
        console.log(e);
      });
    } else {
      location.href = url;
    }
  },
};
